﻿using System.Linq;
using Xenta.Entities;
using Xenta.Utils;

namespace Xenta.Operations
{
    /// <summary>
    /// Represents the profile create operation base.
    /// </summary>
    public abstract class CreateAccountProfile<TProfile> : CreateAuditable<TProfile>
        where TProfile : AccountProfileEntity
    {
        #region Properties

        /// <summary>
        /// Gets or sets the account identifier.
        /// </summary>
        public int AccountID
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Construct(IUnitOfWork uow, TProfile entity)
        {
            base.Construct(uow, entity);

            entity.Account = uow.Execute(new GetAccount
            {
                ThrowsException = true,
                EntityID = AccountID
            }).Result;
        }

        #endregion
    }

    /// <summary>
    /// Represents the profile update operation base.
    /// </summary>
    public abstract class UpdateAccountProfile<TProfile> : UpdateAuditable<TProfile>
        where TProfile : AccountProfileEntity
    {
    }

    /// <summary>
    /// Deletes the account profile.
    /// </summary>
    public class DeleteAccountProfile<TProfile> : DeleteEntity<TProfile>
        where TProfile : AccountProfileEntity
    {
    }

    /// <summary>
    /// Gets the account profile.
    /// </summary>
    public class GetAccountProfile<TProfile> : GetEntity<TProfile>
        where TProfile : AccountProfileEntity
    {
    }

    /// <summary>
    /// Gets the account profile.
    /// </summary>
    public class GetAccountProfileByAccount<TProfile> : QueryEntity<TProfile>
        where TProfile : AccountProfileEntity
    {
        #region Properties

        /// <summary>
        /// Gets or sets the account identifier.
        /// </summary>
        public int AccountID
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override IQueryable<TProfile> Build(IUnitOfWork uow, IQueryable<TProfile> query)
        {
            return base.Build(uow, query).Where(x => x.Account.EntityID == AccountID);
        }

        #endregion
    }
}
